<template>
  <div >
    <div style="background: #D3D3D3; width: 500px; margin: 180px auto;border-radius: 20px; padding: 20px 50px;">
      <h1 style="text-align: center;margin: 30px auto" >注册页面</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>

        <el-form-item label="身份证号">
          <el-input v-model="ruleForm.identityNum"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址">
          <el-input v-model="ruleForm.address"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <template>
            <el-select v-model="ruleForm.role" clearable placeholder="请选择">
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item>
          <el-button plain type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="gotoLogin()">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style>
body{
  background: #ffffff;
}
</style>
<script>
export default {

  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      url:"",
      imageUrl: '',
      options: [{
        value: '0',
        label: '普通用户'
      }, {
        value: '1',
        label: '管理员'
      }, ],
      ruleForm: {
        username:'',
        nickname:'',
        password:'',
        phone:'',
        identityNum:'',
        address:'',
        role:'',

      },
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        announcements: [
          { required: true, message: '请输入注意事项', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    //图片上传方法
    handleSuccess(response,file,fileList){
      this.url = response;
      console.log(response)
    },

    handleRemove(file, fileList) {
      this.url = "";
      console.log(file, fileList);
      //发出删除文件的请求
      // axios.get("/remove?url="+file.response).then(function (response) {
      //   console.log("删除完成!");
      // })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /*向后端提交异步请求*/
          let  url="http://120.77.154.166:8080/user/add-new"

          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$notify({
                title: '成功',
                message: jsonResult.message,
                type: 'success'
              });
              this.resetForm(formName)
            }else {
              this.$notify.error({
                title: '警告',
                message: jsonResult.message,
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    gotoLogin() {
      this.$router.push('/login') // 重定向到登录页面
    },

    /**
     * 上传图片
     * @param
     */
    handleAvatarSuccess(res, file) {
      this.avatar = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }

}
</script>
